package org.example;

import java.util.ArrayList;
import java.util.List;

public class Test1 {
    //leetcode 46 全排列 https://leetcode.cn/problems/permutations/
    boolean[] vis;
    List<List<Integer>> ret;
    List<Integer> path;
    public List<List<Integer>> permute(int[] nums) {
        int n = nums.length;
        vis = new boolean[n];
        ret = new ArrayList<>();
        path = new ArrayList<>();
        dfs(nums);
        return ret;
    }

    private void dfs(int[] nums) {
        if (path.size() == nums.length) {
            ret.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!vis[i]) {
                path.add(nums[i]);
                vis[i] = true;
                dfs(nums);
                path.remove(path.size() - 1);
                vis[i] = false;
            }
        }
    }
}
